package ltd.dujiabao.reserver.util;

import java.io.File;
import java.net.URL;
import java.util.Date;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.Bucket;
import com.qcloud.cos.model.CannedAccessControlList;
import com.qcloud.cos.model.CreateBucketRequest;
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;

/**
 * @author : DuJiabao
 * @Project : reserver
 * @Time : 2021/11/5 23:45
 * @Desc : 腾讯云对象存储服务，进一步封装
 */
public class CosClientUtils {

    /**
     * 执行上传文件操作
     *
     * @param path        文件本地路径
     * @param secretId    密钥id
     * @param secretKey   密钥
     * @param regionName  存储桶区域名字
     * @param bucketName  存储桶名字
     * @param keyInBucket 在存储桶中的key，可以通过该key在存储桶中存取文件
     * @return 文件在对象存储中的url
     */
    public static String doUploadAndGetUrl(String path, String secretId, String secretKey, String regionName,
                                           String bucketName, String keyInBucket) {
        COSClient cosClient = buildClient(secretId, secretKey, regionName);
        uploadFile(path, cosClient, bucketName, keyInBucket);
        return generateDownloadUrl(cosClient, bucketName, keyInBucket);
    }

    /**
     * 选中存储桶中的某个key，并生成对应的url，将url设置为永久
     *
     * @param cosClient   cos客户端
     * @param bucketName  桶名称
     * @param keyInBucket 在存储桶中的key，可以通过该key在存储桶中存取文件
     * @return
     */
    public static String generateDownloadUrl(COSClient cosClient, String bucketName, String keyInBucket) {
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, keyInBucket, HttpMethodName.GET);
        Date expirationDate = new Date(3000, 12, 31);
        req.setExpiration(expirationDate);
        URL url = cosClient.generatePresignedUrl(req);
        return url.toString();
    }

    /**
     * 上传文件
     *
     * @param localFilePath 本地文件路径
     * @param cosClient     cos客户端
     * @param bucketName    桶名称
     * @param keyInBucket   在存储桶中的key，可以通过该key在存储桶中存取文件
     */
    public static void uploadFile(String localFilePath, COSClient cosClient, String bucketName, String keyInBucket) {
        File localFile = new File(localFilePath);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyInBucket, localFile);
        cosClient.putObject(putObjectRequest);
        //TODO: 或许需要做一些结果判断，避免发生错误
    }

    /**
     * 开启一个腾讯云对象存储的客户端
     *
     * @param secretId
     * @param secretKey
     * @param regionName 区域名字
     * @return
     */
    public static COSClient buildClient(String secretId, String secretKey, String regionName) {
        BasicCOSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        return new COSClient(cred, clientConfig);
    }

    /**
     * 新建bucket，如果用现在有的，就不用调这个接口了
     *
     * @param cosClient  cos客户端
     * @param bucketName 存储桶名字
     * @return 存储桶
     */
    public static Bucket buildBucket(COSClient cosClient, String bucketName) {
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setCannedAcl(CannedAccessControlList.PublicReadWrite);
        Bucket bucketResult = null;
        try {
            bucketResult = cosClient.createBucket(createBucketRequest);
        } catch (CosClientException serverException) {
            serverException.printStackTrace();
        }
        return bucketResult;
    }
}
